package com.xk857.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.entities.Card;
import com.xk857.entities.CardDetail;
import com.xk857.school.mapper.CardDetailMapper;
import com.xk857.school.mapper.CardMapper;
import com.xk857.school.service.ICardDetailService;
import com.xk857.util.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-03
 */
@Primary
@Service
public class CardDetailServiceImpl extends ServiceImpl<CardDetailMapper, CardDetail> implements ICardDetailService {

    @Autowired
    private CardMapper cardMapper;

    @Override
    public Result searchByUserId(String userId) {
        QueryWrapper<CardDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<CardDetail> cardDetails = baseMapper.selectList(wrapper);
        System.out.println(cardDetails);
        List<Card> cardList = new ArrayList<>();
        // 如果有数据，则封装card对象，返回给用户
        if (cardDetails.size()>0) {
            for (CardDetail cardDetail : cardDetails) {
                Card card = cardMapper.selectById(cardDetail.getCardUserId());
                System.out.println("=============="+card);
                cardList.add(card);
            }
            return Result.ok(cardList);
        }
        return Result.ok("暂无信息");
    }

    @Override
    public Result userHasRead(String id) {
        CardDetail cardDetail = baseMapper.selectById(id);
        cardDetail.setStatus("2");
        baseMapper.updateById(cardDetail);
        return Result.ok();
    }
}
